//
//  MemoMainViewController.m
//  OfficeEtc
//
//  Created by Wangli on 12-6-2.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "MemoMainViewController.h"
#define SearchBarShowDis  -20.f

@interface MemoMainViewController ()

@end

@implementation MemoMainViewController

-(void) dealloc
{
    [p_DocSearchBar release];
    [p_ListTableView release];
    [super dealloc];
}

-(void) viewWillAppear:(BOOL)animated
{
    UIBarButtonItem* tEditBarBt = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addOneMemo)];
    self.navigationItem.rightBarButtonItem= tEditBarBt;
    [tEditBarBt release];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    p_ListTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.bounds.size.height-44) style:UITableViewStylePlain];
    p_ListTableView.delegate = self;
    p_ListTableView.dataSource = self;
    [self.view addSubview:p_ListTableView];
    
    p_DocSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, -40, 320, 40)];
    p_DocSearchBar.delegate = self;
    [p_ListTableView addSubview:p_DocSearchBar];
    
    p_RegistBtt = [UIButton buttonWithType:UIButtonTypeCustom];
    [p_RegistBtt setBackgroundColor:[UIColor blackColor]];
    p_RegistBtt.frame =CGRectMake(0, 39, 320, self.view.bounds.size.height-84);
    p_RegistBtt.alpha =0.4f;
    [p_RegistBtt addTarget:self action:@selector(registSearchInput) forControlEvents:UIControlEventTouchUpInside];
    p_RegistBtt.hidden =YES;
    [self.view addSubview:p_RegistBtt];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}
#pragma mark-
#pragma mark 类私函数
-(void) registSearchInput
{
    p_RegistBtt.hidden =YES;
    [p_DocSearchBar setShowsCancelButton:NO animated:YES];
    [p_DocSearchBar resignFirstResponder];
    
}

-(void) addOneMemo
{
    
}

#pragma mark-
#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50.f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
#pragma mark-
#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //int row = [indexPath row];
    static NSString* tNameDeCell = @"memocell";
    UITableViewCell* tCell = nil;
    tCell = [tableView dequeueReusableCellWithIdentifier:tNameDeCell];
    if (tCell == nil) {
        tCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tNameDeCell] autorelease];
    }
    return tCell;
}

#pragma mark-
#pragma mark UIScrollViewDelegate
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    NSLog(@"%f",scrollView.contentOffset.y);
    p_NeedDoAnimate =NO;
    if (scrollView.contentOffset.y < SearchBarShowDis) {
        if (decelerate) {
            p_NeedDoAnimate =YES;
        }
        if (scrollView.contentInset.top == 0) {
            [p_ListTableView  setContentInset:UIEdgeInsetsMake(40, 0, 0, 0)];
            
        }
    }else if( scrollView.contentOffset.y >= SearchBarShowDis){
        if (p_ListTableView.contentInset.top != 0) {
            [p_ListTableView  setContentInset:UIEdgeInsetsMake(0, 0, 0, 0)];
        }
    }
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (p_NeedDoAnimate) {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [p_ListTableView setContentOffset:CGPointMake(0, -40)];
        [UIView commitAnimations];
    }
}
#pragma mark-
#pragma mark UISearchBarDelegate
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    [searchBar setShowsCancelButton:YES animated:YES];
    p_RegistBtt.hidden =NO;
    return YES;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    
}
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar{
    p_RegistBtt.hidden =YES;
    [searchBar setShowsCancelButton:NO animated:YES];
    [p_DocSearchBar resignFirstResponder];
}
@end
